package com.haierCamera.customapplication.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.RegisterRepo;
import com.haierCamera.customapplication.databinding.HzklActivityVcodeBinding;
import com.haierCamera.customapplication.utils.EditTextChangeBoolean;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;
import com.haierCamera.customapplication.utils.android.SystemUIHelper;

import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLVcodeActivity extends BaseActivity {
    private HzklActivityVcodeBinding binding;
    private Timer timer = new Timer();
    private int time = 60;
    @Inject
    RegisterRepo registerRepo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemUIHelper.setFullWindowNew(getWindow());
        SystemUIHelper.setStatusBarTextColor(getWindow(), true);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_vcode);
        initData();
    }

    private void initData() {
        String phone = getIntent().getStringExtra("phone");
        String pwd = getIntent().getStringExtra("pwd");
        int type = getIntent().getIntExtra("type", 100);
        binding.forgetBack.setOnClickListener(view -> finish());
        binding.vcodePhonne.setText(String.format("验证码已发送至手机号%s", phone));
        binding.vcodeMin.setOnClickListener(view -> {
            if (time != 60) {
                return;
            }
            sendTime();
            sendCode(phone);
        });
        binding.vcodeBtn.setOnClickListener(view -> {
            String vcode = binding.vcode.getText().toString();
            if (TextUtils.isEmpty(vcode)) {
                Toast.makeText(HZKLVcodeActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                return;
            }
            if (type == 1) {
                vodeSend(phone, pwd, vcode);
            } else {
                vodeSendForget(phone, pwd, vcode);
            }

        });
        sendTime();
        new EditTextChangeBoolean().buttonEnable(binding.vcode, enable -> binding.vcodeBtn.setEnabled(enable));
    }

    private void vodeSend(String phone, String pwd, String vcode) {
        registerRepo.getRegister(phone, vcode, pwd).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(HZKLVcodeActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    Toast.makeText(HZKLVcodeActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        });
    }

    private void vodeSendForget(String phone, String pwd, String vcode) {
        registerRepo.forget(phone, vcode, pwd).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(HZKLVcodeActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    Toast.makeText(HZKLVcodeActivity.this, "找回成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                    break;
            }
        });
    }

    private void sendCode(String phone) {
        registerRepo.getVcode(phone).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    break;
                case ERROR:
                    Toast.makeText(HZKLVcodeActivity.this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS:
                    Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
                    break;
            }
        });
    }

    private void sendTime() {
        MyTask timerTask = new MyTask();
        timer.schedule(timerTask, 100, 1000);
    }

    class MyTask extends TimerTask {

        @Override
        public void run() {
            --time;
            runOnUiThread(() -> binding.vcodeMin.setText(time + "s"));
            if (time == 0) {
                time = 60;
                this.cancel();
                runOnUiThread(() -> binding.vcodeMin.setText("重新获取验证码"));
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
